package com.mrwang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrwang.PageResult;
import com.mrwang.mapper.*;
import com.mrwang.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class GoodsService {
    @Resource
    SpuMapper spuMapper;
    @Resource
    CategoryService categoryService;
    @Resource
    BrandMapper brandMapper;
    @Resource
    SpuDetailMapper spuDetailMapper;
    @Resource
    SkuMapper skuMapper;
    @Resource
    StockMapper stockMapper;
    public PageResult<SpuVo> spuVoPage(String key, Boolean saleable, Integer page, Integer rows) {
        Example example=new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(key)){
            criteria.andLike("title","%"+key+"%");
        }
        if (saleable!=null){
            criteria.andEqualTo("saleable",saleable);
        }
        PageHelper.startPage(page,rows);

        List<Spu> spuList = spuMapper.selectByExample(example);
        PageInfo<Spu> pageInfo = new PageInfo<>(spuList);

        List<SpuVo> list = new ArrayList<>();

        spuList.forEach(spu -> {
            SpuVo spuVo = new SpuVo();

            BeanUtils.copyProperties(spu,spuVo);

            List<String> names= categoryService.namesByIds(Arrays.asList(spu.getCid1(),spu.getCid2(),spu.getCid3()));

            spuVo.setCname(StringUtils.join(names,"/"));

            spuVo.setBname(brandMapper.selectByPrimaryKey(spu.getBrandId()).getName());

            list.add(spuVo);

        });
        return new PageResult<SpuVo>(pageInfo.getTotal(),list);
    }


    @Transactional
    public void saveGoods(SpuVo spuVo) {
        spuVo.setId(null);
        spuVo.setSaleable(true);
        spuVo.setValid(true);
        spuVo.setCreateTime(new Date());
        spuVo.setLastUpdateTime(new Date());
        spuMapper.insertSelective(spuVo);

        SpuDetail spuDetail = spuVo.getSpuDetail();
        spuDetail.setSpuId(spuVo.getId());
        spuDetailMapper.insertSelective(spuDetail);

        List<Sku> skus = spuVo.getSkus();
        if (!CollectionUtils.isEmpty(spuVo.getSkus())){
            saveSkuAndStock(spuVo);
        }
    }

    private void saveSkuAndStock(SpuVo spuVo){
        spuVo.getSkus().forEach(sku -> {
            sku.setSpuId(spuVo.getId());
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(new Date());
            skuMapper.insertSelective(sku);

            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            stock.setStock(sku.getStock());
            stockMapper.insertSelective(stock);
        });
    }


    public SpuDetail spuDetailBySpuId(Long spuId) {
        return spuDetailMapper.selectByPrimaryKey(spuId);
    }

    public List<Sku> skuListBySpuId(Long spuId) {
        Sku sku = new Sku();
        sku.setSpuId(spuId);

        List<Sku> skuList = skuMapper.select(sku);
        skuList.forEach(sku1 -> {
            Stock stock = stockMapper.selectByPrimaryKey(sku1.getId());
            sku1.setStock(stock.getStock());
        });
        return skuList;
    }

    public void updateGoods(SpuVo spuVo) {
        List<Sku> skuList = skuListBySpuId(spuVo.getId());
        if (!CollectionUtils.isEmpty(skuList)){
            List<Long> skuIds = new ArrayList<>();
            for (Sku sku : skuList) {
                skuIds.add(sku.getId());
            }
            Example example = new Example(Stock.class);
            example.createCriteria().andIn("skuId",skuIds);
            stockMapper.deleteByExample(example);

            Sku sku = new Sku();
            sku.setSpuId(spuVo.getId());
            skuMapper.delete(sku);
        }

        saveSkuAndStock(spuVo);

        spuVo.setLastUpdateTime(new Date());
        spuVo.setCreateTime(null);

        spuMapper.updateByPrimaryKeySelective(spuVo);

        spuDetailMapper.updateByPrimaryKeySelective(spuVo.getSpuDetail());

    }
}
